package pl.com.qapps.datastore.berkeley;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ByteKeyComparator implements Comparator<byte[]>, Serializable {

	private static Logger logger = Logger.getLogger(ByteKeyComparator.class.getName());
	/**
	 * 
	 */
	private static final long serialVersionUID = -3910070011526166120L;

	@Override
	public int compare(byte[] array, byte[] other) {
		int result;
		if (array.length > other.length) {
			result = compare(other, array) * -1;
			logger.log(Level.FINE, "		comparing " + Arrays.toString(array) + Arrays.toString(other), result);
			return result;
		}
		for (int i = 0; i < array.length; i++) {
			if (array[i] == other[i])
				continue;
			else {
				result = Byte.compare(array[i], other[i]);
				logger.log(Level.FINE, "		comparing " + Arrays.toString(array) + Arrays.toString(other), result);
				return result;
			}
		}
		result = array.length == other.length ? 0 : -1;
		logger.log(Level.FINE, "		comparing " + Arrays.toString(array) + Arrays.toString(other), result);
		return result;
	}

}
